<?php
/**
 * Created by 784855684@qq.com.
 * User: linzh
 * Date: 7/30/17
 * Time: 1:23 PM
 */


namespace inframe\test\core;

use inframe\core\Cache;
use inframe\core\cache\CacheInterface;
use inframe\core\cache\Redis;
use inframe\core\cache\File;
use inframe\core\cache\Memcached;
use inframe\test\Wsunit;

class CacheTest extends Wsunit
{

    private $types = [
        0 => [
            Redis::class,
        ],
        IN_STORAGE_MEMCACHED => [
            Memcached::class,
        ],
        IN_STORAGE_REDIS => [
            Redis::class,
        ],
        IN_STORAGE_FILE => [
            File::class,
        ],
    ];

    public function testinstance()
    {
        foreach ($this->types as $type => $array) {
            $adapters = Cache::getInstance($type)->getAdapter();
            /** @var CacheInterface $adapter */
            $adapter = $adapters[0];
            $key = microtime(true);
            $adapter->set($key, $key);
            $this->assertEquals($key, $adapter->get($key));
            $this->assertEquals($array[0], $adapters[1]);
        }
    }

    /**
     * 类型测试
     */
    public function testgetsettype()
    {
        foreach ($this->types as $type => $array) {
            $adapters = Cache::getInstance($type)->getAdapter();
            /** @var CacheInterface $adapter */
            $adapter = $adapters[0];
            $key = microtime(true);
            foreach ([
                         0, '', [], true,
                         1122, 'goodtime', false
                     ] as $item) {
                $adapter->set($key, $item);
                $this->assertTrue($item === $adapter->get($key));
            }

            $obj = new \stdClass();
            $obj->key = 'wannabe';
            $adapter->set($key, $obj);
            # 取出来的对象不在同一内存区块,但是属性完全一致
            $this->assertTrue($obj !== $adapter->get($key));
            $this->assertTrue($obj->key === $adapter->get($key)->key);

            $this->assertEquals($array[0], $adapters[1]);
        }
    }

}